import blue
import trinity
import spaceObject
import uthread
import nodemanager
import timecurves
import base
import util
import random
import turret
RACE_MAP = {1: 'csh111',
 2: 'msp111',
 4: 'ase111',
 8: 'gsh211'}

class CombatDroneLight(spaceObject.SpaceObject):
    __guid__ = 'spaceObject.CombatDroneLight'

    def __init__(self):
        spaceObject.SpaceObject.__init__(self)
        self.modules = {}
        self.targets = []
        self.fitted = False
        self.boosters = None
        self.model = None
        self.npcDrone = True



    def LoadModel(self, fileName = None, useInstance = False):
        slimItem = sm.StartService('michelle').GetBallpark().GetInvItem(self.id)
        self.typeID = slimItem.typeID
        if (settings.user.ui.Get('droneModelsEnabled', 1) or (not self.npcDrone)):
            fileName = self.GetTrinityVersionFilename(cfg.invtypes.Get(slimItem.typeID).GraphicFile())
            spaceObject.SpaceObject.LoadModel(self, fileName, useInstance)
        else:
            dummyModel = trinity.EveShip2()
            locator1 = trinity.EveLocator2()
            locator1.name = 'locator_turret_1a'
            locator2 = trinity.EveLocator2()
            locator2.name = 'locator_turret_1b'
            locator2.transform = ((1.0, 0.0, 0.0, 0.0),
             (0.0, -1.0, 0.0, 0.0),
             (0.0, 0.0, 1.0, 0.0),
             (0.0, 0.0, 0.0, 1.0))
            dummyModel.locators.append(locator1)
            dummyModel.locators.append(locator2)
            spaceObject.SpaceObject.LoadModel(self, loadedModel=dummyModel)



    def Assemble(self):
        if not (settings.user.ui.Get('droneModelsEnabled', 1) or (not self.npcDrone)):
            return 
        self.FitBoosters2(alwaysOn=True)
        self.SetupAmbientAudio()
        if hasattr(self.model, 'ChainAnimationEx'):
            self.model.ChainAnimationEx('NormalLoop', 0, 0, 1.0)



    def FitHardpoints(self):
        if self.fitted:
            return 
        if (self.model is None):
            self.LogWarn('FitHardpoints - No model')
            return 
        self.fitted = True
        if not settings.user.ui.Get('turretsEnabled', 1):
            return 
        turretPair = turret.Turret_sc2()
        if (self.model.GetTurretLocatorCount() in (1, 2)):
            to = cfg.invtypes.Get(self.typeID)
            raceID = to.raceID
            groupID = to.groupID
            if (groupID == const.groupFighterBomber):
                eveTurretPair = turretPair.Initialize('dsh112', 'locator_turret_1a', 'locator_turret_1b', overrideBeamGraphicID='FighterBomber_Missile')
            elif (raceID is not None):
                raceDroneTurret = RACE_MAP[raceID]
                eveTurretPair = turretPair.Initialize('dsh111', 'locator_turret_1a', 'locator_turret_1b', overrideBeamGraphicID=raceDroneTurret)
            else:
                eveTurretPair = turretPair.Initialize('dsh111', 'locator_turret_1a', 'locator_turret_1b', overrideBeamGraphicID='rse111')
            self.model.turrets.append(eveTurretPair)
            self.modules[self.id] = turretPair
        else:
            slimItem = sm.StartService('michelle').GetBallpark().GetInvItem(self.id)
            self.LogError('TypeID', slimItem.typeID, 'has no turret locators')
        self.model.RebuildCachedLocatorIndices()



    def Release(self):
        if self.released:
            return 
        if hasattr(self, 'trailNodes'):
            if (self.trailNodes is not None):
                for trail in self.trailNodes:
                    trail.absBall = None

        self.trailNodes = None
        for turretPair in self.modules.itervalues():
            turretPair.owner = None

        self.modules = None
        spaceObject.SpaceObject.Release(self)



    def Explode(self):
        if ((not (settings.user.ui.Get('droneModelsEnabled', 1) or (not self.npcDrone))) or (not settings.user.ui.Get('explosionEffectsEnabled', 1))):
            return False
        else:
            explosionURL = 'res:/Emitter/explosion_end.blue'
            return spaceObject.SpaceObject.Explode(self, explosionURL, absoluteScaling=0.25, scaling=(0.8 + (random.random() * 0.4)), randomRotation=True)



exports = {'spaceObject.CombatDroneLight': CombatDroneLight}

